package com.liu.nb.security;

import cn.hutool.json.JSONUtil;
import com.liu.nb.util.Result;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/*
@name:
@description:这个是处理权限不足的类
@author: lzy
@title: 天王盖地虎!!!
@time:  2021-08-08 16:27:59
*/
@Component
public class JwtAccessDeniedHandler implements AccessDeniedHandler {
    @Override
    public void handle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, AccessDeniedException e) throws IOException, ServletException {
        httpServletResponse.setContentType("application/json;charset=UTF-8");
        ServletOutputStream outputStream=httpServletResponse.getOutputStream();
        httpServletResponse.setStatus(HttpServletResponse.SC_FORBIDDEN);
        Result result=Result.fail(e.getMessage()+":该用户没有权限，请联系管理员。");
        outputStream.write(JSONUtil.toJsonStr(result).getBytes("UTF-8"));
        outputStream.flush();
        outputStream.close();
    }
}
